<template>
  <div class="app-container">
    <div ref="filter" style="margin-bottom: unset" class="filter-container">
      <el-input
        v-model="listQuery.name"
        size="small"
        placeholder="请填写标签名称"
        style="width: 200px;margin-left:5px"
        class="filter-item filter-left"
        clearable
      />
      <el-button
        size="small"
        class="filter-item filter-left"
        icon="el-icon-search"
        type="primary"
        style="margin-left:5px"
        @click="search()"
        plain
      >
        搜索
      </el-button>
      <el-button
        size="small"
        class="filter-item left"
        type="primary"
        icon="el-icon-plus"
        style="margin-left: 5px;"
        @click="addRow()"
      >
        添加标签
      </el-button>
      <el-button size="small" class="filter-item right filter-left filter-refresh" icon="el-icon-refresh" type="primary"
                 plain @click="refresh()"/>
    </div>
    <div ref="filter" class="filter-container">
      <el-button
        size="small"
        class="filter-item left"
        type="danger"
        icon="el-icon-delete"
        style="margin-left: 5px;"
        @click="deleteSelection()"
      >
        一键删除
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      border
      size="medium"
      :height="tableHeight"
      :header-cell-style="{background:'#F2F6FC'}"
      @selection-change="handleSelection"
    >
      <el-table-column type="selection" align="center"/>
      <el-table-column align="center" label="序号" type="index" width="80"/>
      <el-table-column label="名称" prop="name" align="center"/>
      <el-table-column label="颜色" prop="color" align="center"/>
      <el-table-column label="预览" prop="color" align="center">
        <template slot-scope="{row}">
          <el-tag
            :style="{'background-color':row.color,'border-color':row.color}"
            effect="dark">
            {{ row.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="归属人" v-if="currentUserInfo.userType == 3">
        <template slot-scope="{row}">
          {{row.user ? row.user.trueName : '无'}}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" prop="createdAt" align="center"/>
      <el-table-column label="更新时间" prop="updatedAt" align="center"/>
      <el-table-column align="center" fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" plain icon="el-icon-edit" @click="editRow(scope.row.id)">编辑
          </el-button>

          <el-button type="danger" size="mini" plain icon="el-icon-delete" @click="deleteRow(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />
    <Edit :id="id" :show.sync="showState" @closeAction="refresh"/>
  </div>
</template>

<script>
import {deleteCollectLabel, deleteLabel, getList} from "@/api/customer/label";
import Edit from './edit'
import Pagination from '@/components/Pagination'
import { UserModule } from '@/store/modules/user'

export default {
  name: "index",
  components: {Edit, Pagination},
  data() {
    return {
      currentUserInfo:UserModule,
      clientHeight: document.documentElement.clientHeight,
      clientWidth: document.documentElement.clientWidth,
      tableHeight: 'calc(100vh - 165px)',
      listLoading: true,
      listQuery: {
        name: '',
        limit: 20,
        page: 1,
      },
      list: [],
      showState: false,
      id: '',
      total: 0,
      selection: [],
    }
  },
  mounted() {
    this.fetchData()
    window.onresize = () => {
      this.clientHeight = document.documentElement.clientHeight;
      this.clientWidth = document.documentElement.clientWidth;
    }
  },
  updated() {
    this.setTableHeight()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const {data} = await getList(this.listQuery, this.listQuery.page, this.listQuery.limit)
      this.list = data.items
      this.total = data.total
      setTimeout(() => {
        this.listLoading = false
      }, 0.5 * 1000)
    },
    setTableHeight() {
      let height = 205;
      if (this.total) {
        height = 245;
      }
      this.tableHeight = 'calc(100vh - ' + (height) + 'px)'
    },
    refresh() {
      // 刷新重置条件
      this.listQuery = {
        name: '',
        limit: 20,
        page: 1,
      }
      this.showState = false;
      this.fetchData()
    },
    search() {
      this.fetchData()
    },
    addRow() {
      this.id = ''
      this.showState = true
    },
    editRow(id) {
      this.id = id
      this.showState = true
    },
    handleSelection(val) {
      this.selection = val
    },
    deleteSelection() {
      if (this.selection.length <= 0) {
        this.$message({
          type: 'info',
          message: '请勾选需要删除的标签'
        })
        return false;
      }
      this.$confirm('确认要删除勾选的数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.listLoading = true
        let ids = [];
        for (let i in this.selection) {
          ids.push(this.selection[i].id);
        }
        await deleteCollectLabel(ids).then((res) => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }).catch(() => {
        this.fetchData()
      })
    },
    deleteRow(id) {
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.listLoading = true
        await deleteLabel(id).then((res) => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }).catch(() => {
        this.fetchData()
      })
    },
  }
}
</script>

<style scoped>

</style>
